﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Katilgan.Orm
{
    public static class FormatHelper
    {
        public static object AssignValue(object value, Type tip, bool convert = true, bool getDBNull = false)
        {
            if (value == null || value == DBNull.Value) return getDBNull ? value : null;
            if (!convert) return value;

            Type valueType = Nullable.GetUnderlyingType(value.GetType()) ?? value.GetType();


            if (new[] { typeof(double), typeof(float), typeof(decimal) }.Contains(valueType))
            {
                value = Convert.ChangeType((value as IFormattable).ToString("0.00", CultureInfo.CurrentCulture), tip);
            }
            return value;
        }
    }
}
